import {max as _lodashMax,
  min as _lodashMin,
  sortBy as _sortBy} from 'lodash-es'
import * as lodash from 'lodash-es'
export const lodashAssign = (data1,data2)=>{
  if(data2&&data2.dialogCom){
    delete data2.dialogCom;
  }
  return  lodash.assign  (data1,data2)
};
export const lodashGet =  lodash.get  ;
export const lodashGroupBy =  lodash.groupBy  ;
export const lodashMap =  lodash.map  ;
export const lodashPick =  lodash.pick  ;
export const lodashKeys =  lodash.keys  ;
export const lodashFill =  lodash.fill  ;
const lodashFind =  lodash.find  ;
export const lodashDifference =  lodash.difference  ;
export const lodashDifferenceBy =  lodash.differenceBy  ;
export const lodashDetaultto =  lodash.defaultTo ;
export const lodashreduce =  lodash.reduce  ;
export const loadshflatten =  lodash.flatten


/*  console.log(_.isEmpty(0)); //true
  console.log(_.isEmpty(null));//true
  console.log(_.isEmpty(undefined));//true
  console.log(_.isEmpty('  );//true
  console.log(_.isEmpty([]));//true
  console.log(_.isEmpty({}));//true

  */
export const lodashUnion =  lodash.union  ;
export const lodashUnionBy =  lodash.unionBy  ;
export const lodashuniq =  lodash.uniq  ;
export const lodashUniqBy =lodashuniq;
export const uniqBy=lodashuniq;;

export const lodashuniqBy =  lodash.uniqBy  ;

export const lodashEmpty =  lodash.isEmpty  ;

export const lodashTimes =  lodash.times  ;


export const lodashMax=_lodashMax
export const lodashMin=_lodashMin
export const  lodashSortBy=_sortBy

export function getPropWithDefault(obj, keypath, defaultValue) {
  return lodashGet(obj, keypath, defaultValue);
}

function clearObjectValue(obj = {}) {
  if (!obj) {
    return false
  }
  var keys = Object.keys(obj);
  for (let i = 0; i < keys.length; i++) {
    var key = keys[i]
    if (typeof obj[key] === 'string') {
      obj[key] = ''
    }
    if (typeof obj[key] === 'number') {
      obj[key] = 0
    }

  }
}

function isObject(obj) {
  return Object.prototype.toString.call(obj) === '[object Object]'
}

export function lodashFillData(mainobj = {}, data = {}, whiteList = []) {
  var keys = Object.keys(mainobj);
  for (let i = 0; i < keys.length; i++) {
    var key = keys[i]
    if (whiteList && whiteList.includes && whiteList.includes(key)) {
      continue
    }

    /*是 array的 情况*/
    if (mainobj && mainobj[key] && Array.isArray(mainobj[key])) {
      mainobj[key] = data && data[key] || []
    } else if (/*是对象*/isObject(mainobj[key])) {
      if ((!(data && data[key])) || lodashEmpty(data[key])) {
        clearObjectValue(mainobj[key])
      } else {
        mainobj[key] = data && data[key]
      }
    } else {/*默认*/
      mainobj[key] = data && data[key]
    }
  }
}

//data 不存在 不覆盖mainobj的数据
export function lodashFillDataSoft(mainobj = {}, data = {}) {
  var keys = Object.keys(mainobj);
  for (let i = 0; i < keys.length; i++) {
    var key = keys[i]
    if (data[key]) {
      mainobj[key] = data[key]
    }
  }
}

export function pickProps(source, keys = []) {
  return lodashPick(source, keys);
}

export function findOneInArray(array = [], cb) {
  var one = lodashFind(array, cb) || {};
  return one;
}


export function  findOneInArrayPlus(array=[],cb){
  for (let i = 0; i <array.length ; i++) {
    var isyes= cb(array[i],i)
    if(isyes){
      return array[i]
    }
  }
}

export function lodashExit(array = [], cb) {
  var one = lodashFind(array, cb) || {};
  return !lodashEmpty(one);
}

/**
 * @description: 判断是不是数字o
 * @params
 * @return
 */
export function isZeroNumber(number) {
  if (number == '0') return true;
}

//from elementui
export const isEmpty = function (val) {
  // null or undefined
  if (val == null) return true;

  if (typeof val === 'boolean') return false;

  if (typeof val === 'number') return !val;

  if (val instanceof Error) return val.message === '';

  switch (Object.prototype.toString.call(val)) {
    // String or Array
    case '[object String]':
    case '[object Array]':
      return !val.length;

    // Map or Set or File
    case '[object File]':
    case '[object Map]':
    case '[object Set]': {
      return !val.size;
    }
    // Plain Object
    case '[object Object]': {
      return !Object.keys(val).length;
    }
  }

  return false;
};

/**
 * @description: 交集   a b 是一个数组
 * @params
 * @return
 */
export function intersectionArray(a = [], b = []) {
  let intersection = a.filter(v => b.includes(v));
  return intersection;
}

//合并2个数组 并且去重
export function merrayArrayDuplicate(a = [], b = []) {
  var map = {};
  for (let i = 0; i < b.length; i++) {
    if (b[i]) {
      map[b[i]] = b[i];
    }

  }
  for (let i = 0; i < a.length; i++) {
    if (a[i]) {
      map[a[i]] = a[i];
    }
  }
  return lodashMap(map, v => v);
}

/**
 * @description:
 * @params
 * @return  boolean
 */
export function isExit(array = [], func) {
  var one = findOneInArray(array, func)
  return !lodashEmpty(one)
}

export function lodashSame(array1, array2) {
  var samearray = []
  for (let i = 0; i < array1.length; i++) {
    var exit = lodashExit(array2, item2 => item2 == array1[i])
    if (exit) {
      samearray.push(array1[i])
    }
  }
  return samearray
}

export function  lodashToString(_params){
var str=""
  lodashMap(_params,(value,key)=>{
    str+=`${key}:${value}_`
  })
  if(str?.lastIndexOf('-')==0){
    str=str.substr(0,str.length-1)
  }
return str;
}

export const deepClone =lodash.cloneDeep;
